package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.entity.BatteryDataInfoEntity;
import com.hitqz.robot.api.business.vo.BatteryLevelDataVo;
import com.hitqz.robot.api.business.vo.BatteryLevelStatisticVo;
import com.hitqz.robot.biz.mapper.BatteryDataInfoMapper;
import com.hitqz.robot.biz.service.BatteryDataInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xupkun
 * @date 2024/12/17
 */

@Service
@RequiredArgsConstructor
public class BatteryDataInfoServiceImpl  extends ServiceImpl<BatteryDataInfoMapper, BatteryDataInfoEntity> implements BatteryDataInfoService {


    @Override
    public BatteryLevelStatisticVo staticDate(String clientId, String date) {
        LambdaQueryWrapper<BatteryDataInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BatteryDataInfoEntity::getClientId, clientId);
        wrapper.gt(BatteryDataInfoEntity::getStatisticalTime, date + " 00:00:00");
        wrapper.le(BatteryDataInfoEntity::getStatisticalTime, date + " 23:59:59");
        List<BatteryDataInfoEntity> entities = this.list(wrapper
                .orderByAsc(BatteryDataInfoEntity::getStatisticalTime)
        );
        BatteryLevelStatisticVo vo = new BatteryLevelStatisticVo();
        vo.setXAxis(entities.stream().map(
                t -> DateUtil.format(t.getStatisticalTime(), "HH:mm:ss")).collect(Collectors.toList()));
        List<BatteryLevelDataVo> list = new ArrayList<>();
        list.add(new BatteryLevelDataVo(entities.stream().map(BatteryDataInfoEntity::getBattery).collect(Collectors.toList()), "电量"));
        list.add(new BatteryLevelDataVo(entities.stream().map(BatteryDataInfoEntity::getCurrent).collect(Collectors.toList()), "电流"));
        list.add(new BatteryLevelDataVo(entities.stream().map(BatteryDataInfoEntity::getVoltage).collect(Collectors.toList()), "电压"));
        vo.setSeries(list);
        return vo;
    }
}
